// written by Jonathan Odom
#include "EventProcessor.h"
//used to clear output directory
#include <stdio.h>

const string outputDir = "output/";
const string usedOutputDir = "usedOutput/";
EventProcessor::EventProcessor()
{
}

EventProcessor::EventProcessor(const vector<Event*>& newEvents)
{
	if(myEvents.size()==0)
	{
		myEvents=newEvents;	
	}
	else 
	{
		add(newEvents);
	}	
	
	removeDuplicates();
}

void EventProcessor::add(const vector<Event*>& newEvents)
{
	for(unsigned int k=0; k<newEvents.size(); k++)
	{
		myEvents.push_back(newEvents[k]);
	}
	removeDuplicates();
}

void EventProcessor::removeDuplicates()
{
	vector<Event*> newVector;
	vector<Event*>::iterator start = newVector.begin();
	vector<Event*>::iterator end = newVector.end();
	for(unsigned int k=0; k<myEvents.size(); k++)
	{
		start = newVector.begin();
		end = newVector.end();
		if( 0==count(*start,*end, *myEvents[k] ) )
		{
			newVector.push_back(myEvents[k]);
		}
	}
	myEvents=newVector;
	
}


void EventProcessor::generateHTML(HTMLOutputter& out)
{
	//must sort for so output can be be ordered by day
	cerr << "sorting..." ;
	sort((myEvents.begin()), (myEvents.end()), Comparitor<Event*> (new Sorter<Event*>()));
	cerr << "done." << endl;
	Filter eventsFilter(myEvents);
	
	vector<Event*> storeEvents;
	for(unsigned int k = 0; k< 24; k++) {
		storeEvents=eventsFilter.filterTimes(k);
		if(storeEvents.size()!=0)
			out.getTable(storeEvents , "B");
		storeEvents.clear();
	}
	
	for(unsigned int k = 0; k< myEvents.size(); k++) {
		storeEvents=eventsFilter.filterDates(myEvents[k]->getDay());
		if(storeEvents.size()!=0)
			out.getList(storeEvents , "C");
		storeEvents.clear();
	}
	
	out.getTable(myEvents, "");

	for(unsigned int k = 0; k< myEvents.size(); k++) {
		storeEvents=eventsFilter.filterDescriptions(myEvents[k]->getDescription());
		out.getTable(storeEvents , "A");
		storeEvents.clear();
	}
}

